package org.logan.sample.web.main.service.common;

import java.lang.reflect.Type;
import java.util.Map;

import javax.annotation.Resource;

import org.logan.sample.base.utils.json.JsonHandlerGson;
import org.logan.sample.db.model.SysConfig;
import org.logan.sample.web.base.common.RedisConf;
import org.logan.sample.web.main.service.system.SysConfigService;
import org.springframework.stereotype.Component;

import com.google.gson.reflect.TypeToken;

/**
 * @Desc：系统配置
 * @Title：SysConfigs
 * @author: Logan
 * @version: 1.0
 * @date: 2016年12月2日 下午2:33:38
 */
@Component
public class SysConfigs {
	@Resource
	SysConfigService sysConfigService;

	private RedisConf redisConf = null;

	private Map<String, Boolean> SysEnableItems = null;

	public RedisConf getRedisConf() {
		if (null == redisConf) {
			SysConfig sc = sysConfigService.getCfgBykey("redis");
			if (sc != null)
				redisConf = JsonHandlerGson.formJsonStr(sc.getConfigValue(), RedisConf.class);
		}
		return redisConf;
	}

	public void setRedisConf(RedisConf redisConf) {
		this.redisConf = redisConf;
		SysConfig sc = sysConfigService.getCfgBykey("redis");
		if (null == sc) {
			sc = new SysConfig();
			sc.setConfigName("Redis");
			sc.setConfigKey("redis");
			sc.setConfigValue(JsonHandlerGson.ToJsonStr(redisConf));
			sysConfigService.savaCfg(sc);
		} else {
			sc.setConfigValue(JsonHandlerGson.ToJsonStr(redisConf));
			sysConfigService.updateCfgBykey(sc);
		}

	}

	public Map<String, Boolean> getSysEnableItems() {
		if (null == SysEnableItems) {
			SysConfig sc = sysConfigService.getCfgBykey("sysEnableItems");
			if (sc != null) {
				Type t = new TypeToken<Map<String, Boolean>>() {
				}.getType();
				SysEnableItems = JsonHandlerGson.formJsonStr(sc.getConfigValue(), t);
			}
		}
		return SysEnableItems;
	}

	public void setSysEnableItems(Map<String, Boolean> sysEnableItems) {
		SysEnableItems = sysEnableItems;
		SysConfig sc = sysConfigService.getCfgBykey("sysEnableItems");
		if (null == sc) {
			sc = new SysConfig();
			sc.setConfigName("SysEnableItems");
			sc.setConfigKey("sysEnableItems");
			sc.setConfigValue(JsonHandlerGson.ToJsonStr(sysEnableItems));
			sysConfigService.savaCfg(sc);
		} else {
			sc.setConfigValue(JsonHandlerGson.ToJsonStr(sysEnableItems));
			sysConfigService.updateCfgBykey(sc);
		}
	}
}
